package com.aricwang.easyprofile.profile;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.aricwang.easyprofile.contentprovider.ProfileContentProvider;
import com.aricwang.easyprofile.database.ProfileTable;

public class Profile {
	public static final int NOCHANGE 	= -1;
	public static final int SILENT 		= -2;
	public static final int FLIGHT 		= -3;
	public static final int OFF			= 0;
	public static final int ON			= 1;
	
	public static final int DEFAULT_VOLUME = 80;
	public static final int DEFAULT_CELL   = 1;
	public static final int DEFAULT_WIFI   = 0;
	public static final int DEFAULT_BLUETOOTH   = 0;
	public static final int DEFAULT_SCREEN_TIMEOUT   = 30;
	
	// profile id 
	public int mProfileId = -1;
	
	// profile name
	public String mProfileName = "";

	// sound settings
	public int mVolumeCall = NOCHANGE;
	
	// vibrate settings
	public int mVibrate = NOCHANGE;
	
	// wireless settings
	public int mCell = NOCHANGE;
	public int mWifi = NOCHANGE;
	public int mBluetooth = NOCHANGE;

	// screen settings
	public int mTimeout = NOCHANGE;

	
	/////////////////////////////////////////////////////////////////////////////////////////
	// Static functions
	/////////////////////////////////////////////////////////////////////////////////////////

	// get profile contents from database
	static public Profile getProfile(Context context, int id) {
		Profile p = null;
		
		Uri uri = Uri.parse(ProfileContentProvider.CONTENT_URI + "/" + id);
		Cursor c = null;
		try {
			c = context.getContentResolver().query(uri, ProfileTable.ALL_COLUMNS, null, null, null);
		} catch (Exception e) {
			throw new IllegalArgumentException("ERROR");
		}

		if (c == null)
			return p;
		
		if (c.getCount() > 0) {
			// must move to first, otherwise, get exception
			c.moveToFirst();

			p = new Profile();
			
			// get the item from database
			p.mProfileId = c.getInt(c.getColumnIndex(ProfileTable.COLUMN_ID));
			p.mProfileName = c.getString(c.getColumnIndex(ProfileTable.COLUMN_NAME));
			p.mVolumeCall = c.getInt(c.getColumnIndex(ProfileTable.COLUMN_VOLUME_CALL));
			p.mVibrate = c.getInt(c.getColumnIndex(ProfileTable.COLUMN_VIBRATE));
			p.mCell = c.getInt(c.getColumnIndex(ProfileTable.COLUMN_CELL));
			p.mWifi = c.getInt(c.getColumnIndex(ProfileTable.COLUMN_WIFI));
			p.mBluetooth = c.getInt(c.getColumnIndex(ProfileTable.COLUMN_BLUETOOTH));
			p.mTimeout = c.getInt(c.getColumnIndex(ProfileTable.COLUMN_TIMEOUT));
		}
		
		c.close();
			
		return p;
	}
	
	// delete profile from database
	static public int deleteProfile(Context context, int id) {
		Uri uri = Uri.parse(ProfileContentProvider.CONTENT_URI + "/" + id);
		return context.getContentResolver().delete(uri, null, null);
	}
	
	// insert profile to database
	static public Uri insertProfile(Context context, Profile profile) {
		ContentValues values = profileToContentValues(profile);
		return context.getContentResolver().insert(ProfileContentProvider.CONTENT_URI, values);
	}

	// update profile to database
	static public int updateProfile(Context context, Profile profile) {
		Uri uri = Uri.parse(ProfileContentProvider.CONTENT_URI + "/" + profile.mProfileId);
		ContentValues values = profileToContentValues(profile);
		return context.getContentResolver().update(uri, values, null, null);
	}
	
	static private ContentValues profileToContentValues(Profile profile) {
		ContentValues values = new ContentValues();
		values.put(ProfileTable.COLUMN_NAME, profile.mProfileName);
		values.put(ProfileTable.COLUMN_VOLUME_CALL, profile.mVolumeCall);
		values.put(ProfileTable.COLUMN_VIBRATE, profile.mVibrate);
		values.put(ProfileTable.COLUMN_CELL, profile.mCell);
		values.put(ProfileTable.COLUMN_WIFI, profile.mWifi);
		values.put(ProfileTable.COLUMN_BLUETOOTH, profile.mBluetooth);
		values.put(ProfileTable.COLUMN_TIMEOUT, profile.mTimeout);
		
		return values;
	}
	
	static public String getProfileNameById(Context context, int id) {
		String name = "";
		
    	Cursor cursor = context.getContentResolver().query(ProfileContentProvider.CONTENT_URI, 
				new String[] { ProfileTable.COLUMN_ID, ProfileTable.COLUMN_NAME}, 
				ProfileTable.COLUMN_ID + " = " + id, null, null);
		if (cursor != null) {
			if (cursor.getCount() > 0) {
				cursor.moveToFirst();
				name = cursor.getString(cursor.getColumnIndex(ProfileTable.COLUMN_NAME));
			}
			cursor.close();
		}

		return name;
	}
	
}
